﻿define(['util', 'ajax'], function (util, ajax) {
    "use strict";

    var exports = {},
         ViewModel;

    ViewModel = function (model) {
        var self = this;
        var data = model.detail;
        self.id = ko.observable(data.id);
        self.tagId = ko.observable(data.tagId);
        self.code = ko.observable(data.code);
        self.name = ko.observable(data.name);

        self.dateOfBirth = ko.observable(data.dateOfBirth);

        self.address = ko.observable(data.address);
        self.email = ko.observable(data.email);
        self.mobile = ko.observable(data.mobile);
        self.sendEmail = ko.observable(data.sendEmail);
        self.sendSms = ko.observable(data.sendSms);
        self.block = ko.observable(data.block);
        self.usePrint = ko.observable(data.usePrint);

        self.validFrom = ko.observable(data.validFrom);
        self.validTo = ko.observable(data.validTo);

        self.failedAttemptCount = ko.observable(data.failedAttemptCount);
        self.screenMessage = ko.observable(data.screenMessage);
        self.roles = ko.observableArray(model.roles);
        self.role = ko.observable(data.roleName);
        self.userStatuses = ko.observableArray(model.userStatuses);
        self.userStatus = ko.observable(data.userStatusName);
        self.lastLoginTime = data.lastLoginTime == null ? '-' : ko.observable(moment(data.lastLoginTime).format("YYYY-MM-DD HH:MM"));
        self.lastPasswordChangedDate = data.lastPasswordChangedDate == null ? '-' : ko.observable(moment(data.lastPasswordChangedDate).format("YYYY-MM-DD HH:MM"));
        self.imageData = ko.observable(data.imageData);

        self.submit = function () {
            var role = _.find(self.roles(), function (val) {
                return val.name == self.role();
            });

            var userStatus = _.find(self.userStatuses(), function (val) {
                return val.name == self.userStatus();
            });

            var form = {
                Id: self.id(),
                Code: self.code(),
                Name: self.name(),
                DateOfBirth: moment(self.dateOfBirth()).toISOString(),
                Address: self.address(),
                Email: self.email(),
                Mobile: self.mobile(),
                ScreenMessage: self.screenMessage(),
                ValidFrom: moment(self.validFrom()).toISOString(),
                ValidTo: moment(self.validTo()).toISOString(),
                SendSms: self.sendSms(),
                SendEmail: self.sendEmail(),
                UsePrint: self.usePrint(),
                Block: self.block(),
                RoleValue: role == undefined ? '' : role.id,
                UserStatusValue: userStatus == undefined ? '' : userStatus.id,
                ImageData: self.tempImage()
            };
            console.log(self.imageData());
            ajax.put('member', form).done(function (response) {
                if (response.message != '') {
                    window.location = '/member/show?id=' + self.id();
                }
            });
        };

        self.tempImage = ko.observable();

        self.tempImage.subscribe(function (newValue) {
            var canvas = document.getElementById("imgCanvas");
            util.image.drawImage(canvas, newValue);
            self.imageData(canvas.toDataURL());
        });

        self.tempImage(util.image.checkOrDefault(data.imageData));

        self.selectPhoto = function () {
            $("#inputFile").click();
        };

        self.capturePhoto = function () {
            $("#inputFile").click();
        };

        $.subscribe('filesDropped', function (files) {

            if (files.length == 0) return;
            var fileToLoad = files[0];
            var fileReader = new FileReader();
            fileReader.onload = function (fileLoadedEvent) {
                self.tempImage(fileLoadedEvent.target.result);
            };
            fileReader.readAsDataURL(fileToLoad);
        });

        self.cancel = function () {
            window.location = '/member/show?id=' + self.id();
        };
    };

    exports.exec = function (sectionId, model) {
        var viewModel = new ViewModel(model);
        ko.applyBindings(viewModel, document.getElementById(sectionId));
    };

    return exports;
});